﻿<CodeSnippetTabbed DocLink="https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxDataGrid-1.SelectionMode#multiple-selection-with-optimized-performance">
    <CodeSnippetTabPage Text="Razor">@(@"<DxDataGrid Data=""@DataSource""
            @ref=""@grid""
            KeyFieldName=""Id""
            SelectionMode=""DataGridSelectionMode.OptimizedMultipleSelection""
            SelectAllMode=""DataGridSelectAllMode.AllPages""
            OptimizedMultipleSelectionChanged=""OnSelectionChanged""
            CssClass=""mw-1100"">
    <DxDataGridSelectionColumn></DxDataGridSelectionColumn>
    <DxDataGridColumn Field=""@nameof(ProductFlat.ProductName)"" Caption=""Product Name""></DxDataGridColumn>
    <DxDataGridColumn Field=""@nameof(ProductFlat.Category)""></DxDataGridColumn>
    <DxDataGridColumn Field=""@nameof(ProductFlat.Subcategory)""></DxDataGridColumn>
</DxDataGrid>

@code {
    DxDataGrid<ProductFlat> grid;
    bool gridInitialized;

    IEnumerable<ProductFlat> DataSource;

    int SelectedCount { get; set; }

    int StoredSelectedCount { get; set; }

    int StoredUnselectedCount { get; set; }

    protected override async Task OnInitializedAsync() {
        DataSource = await FlatProductService.LoadAsync();
    }

    protected override void OnAfterRender(bool firstRender) {
        if (!gridInitialized && grid != null) {
            gridInitialized = true;

            foreach (var product in DataSource.Take(10).Where((p, i) => i % 2 != 0))
                grid.SetDataRowSelected(product, true);
        }
    }

    protected async Task OnSelectionChanged(DataGridSelection<ProductFlat> selection) {
        StoredSelectedCount = selection.SelectedKeysStored.Count();
        StoredUnselectedCount = selection.UnselectedKeysStored.Count();
        var selectedKeys = await selection.SelectedKeys;
        SelectedCount = selectedKeys.Count();
        await InvokeAsync(StateHasChanged);
    }
}")</CodeSnippetTabPage>
</CodeSnippetTabbed>
